﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Web.Configuration;

namespace IISLogAnalyzer.Data.Providers
{
    public class DataProviderManager
    {
        private static DataProvider defaultProvider;
        private static DataProviderCollection providers;        

        static DataProviderManager()
        {
            Initialize();
        }

        private static void Initialize()
        {
            DataProviderConfiguration configuration =
                (DataProviderConfiguration)
                ConfigurationManager.GetSection("WebLogProvider");

            if (configuration == null)
                throw new ConfigurationErrorsException
                    ("WebLogProvider configuration section is not set correctly.");

            providers = new DataProviderCollection();

            ProvidersHelper.InstantiateProviders(configuration.Providers
                , providers, typeof(DataProvider));

            providers.SetReadOnly();

            defaultProvider = providers[configuration.Default];

            if (defaultProvider == null)
                throw new Exception("defaultProvider");
        }

        public static DataProvider Provider
        {
            get
            {
                return defaultProvider;
            }
        }

        public static DataProviderCollection Providers
        {
            get
            {
                return providers;
            }
        }

    }
}
